package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {
    @Resource
    private AttrMapper attrMapper;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;
    @Autowired
    private SpuAttrValueMapper spuAttrValueMapper;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> selectAttrGroup(Long cid) {
        //根据分类ID查询分组
        List<AttrGroupEntity> entityList = this.list(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCategoryId, cid));
        if (CollectionUtils.isEmpty(entityList)){
            return null;
        }
        //查询组下面的规格参数
        entityList.forEach(en->{
            List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>()
                    .eq(AttrEntity::getGroupId, en.getId())
                    .eq(AttrEntity::getType,1));
            en.setAttrEntities(attrEntities);
        });
        return entityList;
    }

    @Override
    public List<ItemGroupVo> queryGroupWithAndValueByCidAndSpuIdAndSkuId(Long cid, Long spuId, Long skuId) {
        //根据分类id查询分组
        List<AttrGroupEntity> attrGroupEntities =
                this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCategoryId, cid));
        if (CollectionUtils.isEmpty(attrGroupEntities)){
            return null;
        }

        //根据分组查询租下的规格参数
        List<ItemGroupVo> itemGroupVos = attrGroupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVo itemGroupVo = new ItemGroupVo();
            itemGroupVo.setId(attrGroupEntity.getId());
            itemGroupVo.setName(attrGroupEntity.getName());

            //遍历分组查询组下的规格参数
            List<AttrEntity> attrEntities =
                    attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getGroupId, attrGroupEntity.getId()));
            if (CollectionUtils.isEmpty(attrEntities)) {
                return itemGroupVo;
            }

            itemGroupVo.setAttrs(

                    attrEntities.stream().map(attrEntity -> {

                        AttrValueVo attrValueVo = new AttrValueVo();
                        //判断当前规格参数是否是基本类型的规格参数
                        if (attrEntity.getType() == 1) {
                            //如果是基本数据类型的参数
                            SpuAttrValueEntity spuAttrValueEntity = spuAttrValueMapper.selectOne(
                                    new QueryWrapper<SpuAttrValueEntity>().eq("spu_id",spuId)
                                            .eq("attr_id",attrEntity.getId())
                            );
                            BeanUtils.copyProperties(spuAttrValueEntity, attrValueVo);
                        } else {
                            SkuAttrValueEntity skuAttrValueEntity = skuAttrValueMapper.selectOne(
                                    new LambdaQueryWrapper<SkuAttrValueEntity>().eq(SkuAttrValueEntity::getSkuId, skuId)
                                            .eq(SkuAttrValueEntity::getAttrId, attrEntity.getId()));
                            BeanUtils.copyProperties(skuAttrValueEntity, attrValueVo);
                        }

                        return attrValueVo;
                    }).collect(Collectors.toList()));

            return itemGroupVo;
        }).collect(Collectors.toList());


        return itemGroupVos;
    }

}